var prefix = ctx + "outAndStorage/outAndStorageRecord";

$(function() {
	$("#dprtcode").val(window.parent.document.getElementById("dprtcode").value);
	
    var options = {
        url: prefix + "/list",
        striped: true,
        search:false,	
        showSearch:false,	
        showRefresh:false,	
        showToggle:false,	
        showColumns:false,	
        queryParams: queryParams,
        modalName: "出入库记录",
      	onLoadSuccess:function(data){
			signByKeyword($("#keyword").val(), [2,3,4,5,7,9]);// 标记关键字
			$.table.onLoadSuccess(data);
        },
        columns: [
		{
		    title: "序号",
		    formatter: function (value, row, index) {
		 	    return $.table.serialNumber(index);
		    }
		},
		{
        	field: 'unitid',
        	title: '<span style="color:red" >*</span>工具编号',
        	sortable: true,
            formatter: function(value, row, index) {
            	return $.table.tooltip(value);
            }
        },
		 {
        	field: 'toolName',
        	title: '<span style="color:red" >*</span>工具名称',
        	sortable: true,
        	formatter: function(value, row, index) {
        		return $.table.tooltip(value);
        	}
        },
/*	    {
	    	field: 'optype',
	    	title: '类型',
	    	sortable: true,
	        formatter: function (value, row, index) {
	            return $.table.selectDict(outAndStorage_type, value);
	        }
	    },*/
        {
        	field: 'outOperator',
        	title: '<span style="color:red" >*</span>领用人',
        	sortable: true
        },
        {
        	field: 'outTime',
        	title: '领用时间',
        	sortable: true
        },
        {
        	field: 'inOperator',
        	title: '<span style="color:red" >*</span>归还人',
        	sortable: true
        },
        {
        	field: 'inTime',
        	title: '归还时间',
        	sortable: true
        },
        {
        	field: 'zydd',
        	title: '作业地点',
        	sortable: true
        },
        ]
    };
    $.table.init(options);
    
    loadbztt(); //柱状图
    loadbzt(); //初始化饼状图
});

function queryParams(params) {
    return {
    	dprtcode:$("#dprtcode").val(),
        pageSize: params.limit,
        pageNum: params.offset / params.limit + 1,
        searchValue: params.search,
        orderByColumn: params.sort,
        isAsc: params.order
    };
}


var json3 = {
		name: [],
		toolsNums: []
	};
function loadbztt() {
	var obj = {};
    obj.dprtcode =  $("#dprtcode").val();
    $.ajax({
        cache: true,
        type: "POST",
        url: prefix + "/getChartztData",
        async: false,
        data: obj,
        error: function (request) {
            $.modal.alertError("系统错误");
        },
        success: function (data) {
        	dom3 = document.getElementById("container3");
        	myChart3 = echarts.init(dom3);
            json3 = {
            		name: [],
            		toolsNums: []
            };
        
            if (data.length > 0) { 
            
            		 $.each(data, function (index, row) {
            			 json3.name.push(row.name);
            			 json3.toolsNums.push(row.toolsNums);
                     })
//                     json3.toolsNums=data;
            }
            initchars();
        }
    });
}


function initchars() {  
	option3 = {
		title: {
	        text: '工器具使用TOP5统计图',
	        textStyle: {
                fontWeight: 'normal',             
                color: '#00f6ff',  //标题颜色
                fontSize:20
	        },
	    },
	    color: ['#3398DB'],
	    tooltip: {
	        trigger: 'axis',
	        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
	            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
	        }
	    },
	    grid: {
	    	left: '3%',
	        right: '4%',
	        bottom: '3%',
	        y2:140,
	        containLabel: true
	    },
	    textStyle:{//图例文字的样式
            color:'#fff',
            fontSize:12
        },
	    xAxis: [
	        {
	            type: 'category',
	            data: json3.name,
	            axisLabel:{
				     interval:0,//横轴信息全部显示
				     rotate:-10,//-30度角倾斜显示
				},
	            axisTick: {
	                alignWithLabel: true
	            }
	        
	        }
	    ],
	    yAxis: [
	        {
	            type: 'value'
	        }
	    ],
	    series: [
	        {
	            name: '使用次数',
	            type: 'bar',
	            barWidth: '30%',
	            data: json3.toolsNums,
	            itemStyle: {
		              normal: {
		                label: {
		                  formatter: "{c}",
		                  show: true,
		                  position: "top",
		                  textStyle: {
		                    fontWeight: "bolder",
		                    fontSize: "12",
		                    color: "#fff"
		                  }
		                },
		                opacity: 1
		              }
		            }
	        }

	    ]
	};
	;
	if (option3 && typeof option3 === "object") {
		    myChart3.setOption(option3, true);
		}
}






var json2 = {
		name: [],
		toolsNums: []
	};
function loadbzt() {
	var obj = {};
    obj.dprtcode =  $("#dprtcode").val();
    $.ajax({
        cache: true,
        type: "POST",
        url: prefix + "/getChartData",
        async: false,
        data: obj,
        error: function (request) {
            $.modal.alertError("系统错误");
        },
        success: function (data) {
        
        	dom2 = document.getElementById("container2");
        	myChart2 = echarts.init(dom2);
            json2 = {
            		name: [],
            		toolsNums: []
            };
        
            if (data.length > 0) { 
            		 var num=0;
            		 var max = data[0].outnums;
            		 var max1 = data[0].innums;
            		 $.each(data, function (index, row) {
            			 json2.name.push(row.name);
            			 num+=parseInt(row.value);
            			 
            			  var cur = parseInt(row.outnums);
            			   cur > max ? max = cur : null
            					   
            			  var cur1 = parseInt(row.innums);
            			   cur1 > max1 ? max1 = cur1 : null
                     })
                     
                     var name = arrayLookup(data,'outnums',max,'name');
            		 var name1 = arrayLookup(data,'innums',max1,'name');
            		 
            		  $("#outren").text(name);
            		  $("#inren").text(name1);
            		 function arrayLookup(data,key,value,targetKey){
            			    var targetValue = "";
            			    for (var i = 0; i < data.length; i++) {
            			        if(data[i][key]==value){
            			            targetValue = data[i][targetKey];
            			            break;
            			        }
            			    }
            			    return targetValue;
            			}
            		 
                     
            		 
                     $("#nums").text(num);
                     json2.toolsNums=data;
            }
            initecharts1();
        }
    });
}





function initecharts1() {
	option2 = {
			title: {
		        text: '出勤统计',
//		        left:'center',
		        x :70,
				y:40,
		        textStyle: {
	                fontWeight: 'normal',             
	                color: '#00f6ff',  //标题颜色
	                fontSize:20
		        },
		    },
		    tooltip: {
		        trigger: 'item',
		        formatter:"{b}: {c}次 ({d}%)",
		    },
		    legend: {
		        orient: 'vertical',
		        x: 'right',
		        data:json2.name,
		        textStyle:{//图例文字的样式
	                color:'#fff',
	                fontSize:12
	            },
		    },
		    series: [{
		        name: '出勤统计',
		        type: 'pie',
		        selectedMode: 'single',
		        radius: [0, '80%'],
		        label: {
		            normal: {
		                position: 'inner',
		                formatter: function(params, ticket, callback) {
		                    return params.name + "\n\n" + params.percent + "%";
		                }
		            }
		        },
		        labelLine: {
		            normal: {
		                show: false
		            }
		        },
		        color: ['#756b6b', '#00d3ff', '#e87a2c','green','blue','yellow','red','black'],
		        data: json2.toolsNums
		    }]
		};
	  if (option2 && typeof option2 === "object") {
	        myChart2.setOption(option2, true);
	    }
}

